// pages/ownerOertification/ownerOertification.js

var app = getApp();
var util = require('../../utils/util.js');
var applet_park_api_url = app.globalData.applet_park_api_url;
var applet_park_api_url_img = app.globalData.applet_park_api_url_img;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    name:'',
    phone:'',
    profile:'',
    legalPersonIdCardNo:'',
    legalPersonIdCardNo:'',
    legalPersonName:'',
    contactName:'',
    contactPhone:'',
    businessLicenseImage:'',
    businessLicenseNo:'',
    choicetype:'',
    choicescale:'',
    registerAddress:'',
    companyindex: 0,
    companyType: ['请选择公司类型', '民营', '国企', '合资', '其他'],
    companyscaleindex: 0,
    companyscale: ['请选择公司规模', '0-20人', '20-100人', '100-500人', '100人以上'],
    listareaid: [null, 'personal_use', 'business_use', 'b2p_use', 'rental_use'],
    listareaindex: 0,
    listarea: ['请选择', 'A区', 'B区', 'C区', 'D区'],
    listareaid: [null, 'personal_use', 'business_use', 'b2p_use', 'rental_use'],
    detailInfo:{
      pic1:null
    },
    Positive: '',
    unPositive: '',
    detailInfo: {
      pic1: null,
      pic2: null,
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  //选择
  changeUsedType: function (e) {
    
    this.data.areaId = this.data.listareaid[e.detail.value];
    this.data.listareaindex = e.detail.value;
    this.setData(this.data);
  },
  //规模
  changeScale: function (e) {
    
    this.data.choicescale = this.data.companyscale[e.detail.value];
    this.data.companyscaleindex = e.detail.value;
    this.setData(this.data);
  },
  //类型
  changecanmanytype: function (e) {
    
    this.data.choicetype = this.data.companyType[e.detail.value];
    this.data.companyindex = e.detail.value;
    this.setData(this.data);
  },
  bindKeyInput: function (e) {
    this.data[e.currentTarget.dataset.inputname] = e.detail.value;
    this.setData(this.data);
  },
  chooseImage: function (e) {

    var that = this;
    let pic = e.currentTarget.dataset.pic;
    
    wx.chooseImage({
      count: 1,
      success: function (res) {
        that.data.detailInfo[pic] = res.tempFiles[0];

        that.setData({
          detailInfo: that.data.detailInfo
        });
        
      }
    })
  },
  checkParams: function () {
    if (!this.data.nmae) {
      util.showModal('请输入公司名称');
      return false;
    }
    if (!this.data.businessLicenseNo) {
      util.showModal('请输入税号');
      return false;
    }
    if (!this.data.legalPersonIdCardNo) {
      util.showModal('请输入法人证件号码');
      return false;
    }
    if (!this.data.phone) {
      util.showModal('请输入联系电话');
      return false;
    }
    return true;
  },
  submit(){
   

    if (!this.checkParams()) {
      return;
    }
    var that = this;
    var id = wx.getStorageSync("id");
    if (that.data.choicetype =="请选择公司类型"){
      that.data.choicetype=="";
    }
    if (that.data.choicescale == "请选择公司规模") {
      that.data.choicescale == "";
    }
    that.setData(that.data);
    wx.request({
      url: applet_park_api_url + '/api/enterprises',
      data:{
        responsiblePersonId: 1 ,
        name: that.data.name,
        phone: that.data.phone,
        profile: that.data.profile,
        legalPersonIdCardNo: that.data.legalPersonIdCardNo,
        legalPersonIdCardNo: that.data.legalPersonIdCardNo,
        legalPersonName: that.data.legalPersonName,
        contactName: that.data.contactName,
        contactPhone: that.data.contactPhone,
        businessLicenseImage: that.data.businessLicenseImage,
        businessLicenseNo: that.data.businessLicenseNo, 
        registerAddress:'',
        choicetype: that.data.choicetype, 
        choicescale: that.data.choicescale, 
      },
      method:"POSt",
      success(res) {
        
     
        if (res.data.code == 0) {
        
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  loadAreas() {
    var that = this;
    wx.request({
      url: applet_park_api_url + '/api/areas',
      success(res) {
        
        let listarea = [];
        listarea.push("请选择区域")
        let listareaid = [];
        listareaid.push("null")
        if (res.data.code == 0) {
          for (let item of res.data.data) {
            
            listarea.push(item.name)
            listareaid.push(item.id)
          }
          

          that.data.listarea = listarea;
          that.data.listareaid = listareaid;
          that.setData(that.data);
        } else {
          wx.showToast({
            title: '网络异常',
          })
        }
      },
      fail(res) {
        wx.showToast({
          title: res.data,
        })
      }
    })
  },
  imgUpLoad() {
    wx.showLoading({
      title: '图片上传中',
    })
    var that = this;
    const detailInfo = that.data.detailInfo;

    for (var idx = 1; idx <= 2; idx++) {
      let file = detailInfo['path' + idx];
      
      if (file && file.path) {
        
        wx.uploadFile({
          url: applet_park_api_url_img+'/api/upload',
          filePath: file.path,
          name: 'files',
          formData: {
            'user': 'test'
          },
          success: function (res) {
            const response = JSON.parse(res.data)
            if (response.code == 0) {
              
              // 
              // that.detailInfo.data.picUrl = "https://njdhy.com.cn:7777" + response.data[0];
              // that.setData({
              //   detailInfo: file
              // })
              wx.hideLoading();
              
            } else {
              wx.showToast({
                title: '图片上传失败',
              })
              wx.hideLoading();
            }

          },
          fail: function (err) {
            
          }
        })
      }
    }



  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  upload: function () {
    var that = this;
    wx.showLoading({
      title: '图片保存中',
      mask: true
    });

    var finished = true;
    for (var j = 1; j <= 18; j++) {
      var pic = that.data.detailInfo['pic' + j];
      if (pic && pic.path && !pic.picUrl) { //false
        finished = false;
        break;
      }
    }
    if (finished) {
      wx.hideLoading();
      // if (that.data.detailInfo.pic14) {
      //   that.btn();
      // }
      // // else {
      // //   that.submit();
      // // }
      // that.submit();
      return;
    }
    //用自执行函数,来解决循环内部的异步回调问题
    (function uploadImage(i) {
      if (i > 5) {
        return;
      }
      
      // var image = that.data.imageList[i];
      if (that.data.detailInfo['pic' + i] && that.data.detailInfo['pic' + i].path && !that.data.detailInfo['pic' + i].picUrl) {
        var path = that.data.detailInfo['pic' + i].path;
        wx.uploadFile({
          url: 'https://jinchengit.cn:9999/api/upload',
          filePath: path,
          name: 'files',
          success: function (res) {
            const response = JSON.parse(res.data)
            if (response.code == 0) {
              that.data.detailInfo['pic' + i].picUrl = "http://zjurainforest.com:9999" + response.data[0];
              that.setData(that.data)

              
            } else {
              wx.showToast({
                title: '图片上传失败',
              })
              wx.hideLoading();
            }
            
            // that.data.detailInfo['pic' + i].picUrl = "/" + res;
            var allFinished = true;
            for (var j = 1; j <= 4; j++) {
              var pic = that.data.detailInfo['pic' + j];
              if (pic && pic.path && !pic.picUrl) { //false
                allFinished = false;
                break;
              }
            }
            if (allFinished) { //全部传完了,则隐藏加载按钮并返回

              wx.hideLoading();
              that.submit();
            } else {
              uploadImage(i + 1);
            }
          },
          fail(res) {
            wx.hideLoading();
            util.showModal('网络异常,请稍后重试');

          }
        })
      } else {
        uploadImage(i + 1);
      }
    })(0);
  },
})